﻿Module m_SyncConfig

    Public Sub SyncRegister(response() As String, ConsoleSocket As Connections)
        'Receive Synchronization Register Request to Server
        If response(3).Equals("") = False Then
            'Response Code
            '1=Invalid Master ID
            '2=Contact Not Registered
            '3=Failed to Link Slave to Master
            '9=Cannot Complete Request Because an Error Happened

            'Cek syncmaster id apakah ada?
            Dim obj As Object = mysql_query("SELECT * FROM tb_syncconfig WHERE id='" & response(3) & "'")
            If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                Dim TData As DataTable = obj
                If TData.Rows.Count > 0 Then
                    'Cek apakah user pengirim sudah ada pada tabel contact
                    obj = mysql_query("SELECT * FROM tb_contact WHERE email='" & response(1) & "'")
                    If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                        TData = obj
                        If TData.Rows.Count > 0 Then
                            'Available
                            'Cek pada tabel tb_syncconfig_link apakah user pengirim dengan konfigurasi master_id sudah ada?
                            Dim id_kontak As Long = TData.Rows(0)("id_kontak")
                            obj = mysql_query("SELECT * FROM tb_syncconfig_link WHERE id_syncconfig='" & response(3) & "' AND id_kontak='" & id_kontak & "'")
                            If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                                Dim sysDate As String = "0000-00-00 00:00:00"
                                Dim exec_id As String = "0"
                                TData = obj
                                obj = mysql_query("SELECT b.host,b.port,b.username,b.password,a.database FROM tb_syncconfig AS a INNER JOIN tb_host AS b ON a.id_host=b.id_host WHERE a.id='" & response(3) & "'")
                                If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                                    Dim DataHost As DataTable = obj

                                    If DataHost.Rows.Count > 0 Then
                                        obj = mysql_query(String.Format("SELECT execution_id,timestamp FROM auditlog_{0} ORDER BY timestamp DESC LIMIT 1", DataHost(0)("database")), -1, False, DataHost(0)("host"), Convert.ToInt32(DataHost(0)("port")), DataHost(0)("username"), DataHost(0)("password"), DataHost(0)("database"))
                                        If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                                            DataHost = obj
                                            If DataHost.Rows.Count > 0 Then
                                                sysDate = MinDate(DataHost(0)("timestamp").ToString)
                                                exec_id = DataHost(0)("execution_id").ToString
                                            End If
                                        End If
                                    End If

                                    If TData.Rows.Count <= 0 Then
                                        obj = mysql_non_query(String.Format("INSERT INTO tb_syncconfig_link(id_syncconfig,id_kontak,last_timestamp,last_executionid,synchronized) VALUES('{0}','{1}','{2}','{3}','{4}')", response(3), id_kontak, sysDate, exec_id, 0))
                                    Else
                                        obj = mysql_non_query(String.Format("UPDATE tb_syncconfig_link SET last_timestamp='{0}',last_executionid='{1}',synchronized='0' WHERE id_syncconfig='{2}' AND id_kontak='{3}'", sysDate, exec_id, response(3), id_kontak))
                                    End If

                                    If obj Then
jmpError_03:
                                        ConsoleSocket.sendMessages("/dbsyncregisterresponse" & Packet.pSplit & "0" & Packet.pSplit & sysDate, response(1))
                                    Else
                                        ConsoleSocket.sendMessages("/dbsyncregisterresponse" & Packet.pSplit & "3", response(1))
                                    End If
                                Else
                                    GoTo jmpError_03
                                End If
                            Else
                                GoTo jmpError_01
                            End If
                        Else
                            'Not Available
                            ConsoleSocket.sendMessages("/dbsyncregisterresponse" & Packet.pSplit & "2", response(1))
                        End If

                    Else
                        GoTo jmpError_01
                    End If
                Else
jmpError_02:
                    ConsoleSocket.sendMessages("/dbsyncregisterresponse" & Packet.pSplit & "1", response(1))
                End If
            Else
jmpError_01:
                ConsoleSocket.sendMessages("/dbsyncregisterresponse" & Packet.pSplit & "9", response(1))
            End If

        End If
    End Sub

    Public Sub SyncGetConfig(response() As String, ConsoleSocket As Connections)
        'Response Format for Request Synchronization Configuration Data
        'dbname*id_syncconfig*tb_1~tb_2~tb_3#dbname*id_syncconfig*tb_1~tb_2~tb_3
        Dim obj As Object = mysql_query("SELECT a.* FROM tb_syncconfig AS a")
        Dim data As DataTable
        Dim tb As DataTable
        Dim str As String = ""

        If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
            data = obj
            For i As Long = 0 To data.Rows.Count - 1
                obj = mysql_query("SELECT * FROM tb_syncconfig_detail WHERE id_syncconfig='" & data(i)("id") & "'")
                If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                    tb = obj
                    For j As Long = 0 To tb.Rows.Count - 1
                        If j = 0 Then str = str & data(i)("database") & "*" & data(i)("id") & "*"
                        str = str & tb.Rows(j)("table_name")
                        If j < tb.Rows.Count - 1 Then
                            str = str & "~"
                        Else
                            If i < data.Rows.Count - 1 Then
                                str = str & "#"
                            End If
                        End If
                    Next
                Else
                    GoTo SyncConfigResp_01
                    Exit For
                End If
            Next
            ConsoleSocket.sendMessages("/dbsyncconfigresponse" & Packet.pSplit & str, response(1))
        Else
SyncConfigResp_01:
            ConsoleSocket.sendMessages("/dbsyncconfigresponse" & Packet.pSplit & "0", response(1))
        End If
    End Sub
End Module
